package com.tree.backend.service.impl;

import com.tree.backend.entity.vo.CustomerAnalysisVO;
import com.tree.backend.entity.vo.EquipmentAnalysisVO;
import com.tree.backend.mapper.AnalysisMapper;
import com.tree.backend.service.IAnalysisService;
import com.tree.backend.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据分析服务实现
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AnalysisServiceImpl implements IAnalysisService {

    private final AnalysisMapper analysisMapper;

    @Override
    public CustomerAnalysisVO getCustomerAnalysisData() {
        try {
            Integer companyId = UserContext.getCompanyId();
            log.info("获取客户分析数据，公司ID: {}", companyId);
            
            CustomerAnalysisVO vo = new CustomerAnalysisVO();
            
            // 获取客户类型分布
            vo.setCustomerTypeDistribution(mapDistributionData(analysisMapper.getCustomerTypeDistribution(companyId)));
            
            // 获取需求类型分布
            vo.setDemandTypeDistribution(mapDistributionData(analysisMapper.getDemandTypeDistribution(companyId)));
            
            // 获取需求状态分布
            vo.setDemandStatusDistribution(mapDistributionData(analysisMapper.getDemandStatusDistribution(companyId)));
            
            // 获取需求紧急程度分布
            vo.setUrgencyLevelDistribution(mapDistributionData(analysisMapper.getUrgencyLevelDistribution(companyId)));
            
            // 获取客户状态分布
            vo.setCustomerStatusDistribution(mapDistributionData(analysisMapper.getCustomerStatusDistribution(companyId)));
            
            // 获取区域用户数量
            vo.setZoneUserCountData(mapZoneData(analysisMapper.getZoneUserCount(companyId)));
            
            log.info("客户分析数据获取成功");
            return vo;
        } catch (Exception e) {
            log.error("获取客户分析数据失败", e);
            throw new RuntimeException("获取客户分析数据失败", e);
        }
    }

    @Override
    public EquipmentAnalysisVO getEquipmentAnalysisData() {
        try {
            Integer companyId = UserContext.getCompanyId();
            log.info("获取设备分析数据，公司ID: {}", companyId);
            
            EquipmentAnalysisVO vo = new EquipmentAnalysisVO();
            
            // 获取设备类型分布
            vo.setEquipmentTypeDistribution(mapEquipmentDistributionData(analysisMapper.getEquipmentTypeDistribution(companyId)));
            
            // 获取区域总供电
            vo.setZoneCapacityData(mapZoneCapacityData(analysisMapper.getZoneCapacity(companyId)));
            
            log.info("设备分析数据获取成功");
            return vo;
        } catch (Exception e) {
            log.error("获取设备分析数据失败", e);
            throw new RuntimeException("获取设备分析数据失败", e);
        }
    }
    
    // 映射客户分布数据
    private List<CustomerAnalysisVO.DistributionData> mapDistributionData(List<Map<String, Object>> mapList) {
        List<CustomerAnalysisVO.DistributionData> result = new ArrayList<>();
        if (mapList == null || mapList.isEmpty()) {
            return result;
        }
        
        for (Map<String, Object> map : mapList) {
            CustomerAnalysisVO.DistributionData data = new CustomerAnalysisVO.DistributionData();
            data.setType(((Number) map.get("type")).intValue());
            data.setCount(((Number) map.get("count")).intValue());
            result.add(data);
        }
        
        return result;
    }
    
    // 映射区域数据
    private List<CustomerAnalysisVO.ZoneData> mapZoneData(List<Map<String, Object>> mapList) {
        List<CustomerAnalysisVO.ZoneData> result = new ArrayList<>();
        if (mapList == null || mapList.isEmpty()) {
            return result;
        }
        
        for (Map<String, Object> map : mapList) {
            CustomerAnalysisVO.ZoneData data = new CustomerAnalysisVO.ZoneData();
            data.setZoneName((String) map.get("zoneName"));
            data.setCount(((Number) map.get("count")).intValue());
            result.add(data);
        }
        
        return result;
    }
    
    // 映射设备分布数据
    private List<EquipmentAnalysisVO.DistributionData> mapEquipmentDistributionData(List<Map<String, Object>> mapList) {
        List<EquipmentAnalysisVO.DistributionData> result = new ArrayList<>();
        if (mapList == null || mapList.isEmpty()) {
            return result;
        }
        
        for (Map<String, Object> map : mapList) {
            EquipmentAnalysisVO.DistributionData data = new EquipmentAnalysisVO.DistributionData();
            data.setEquipmentType(((Number) map.get("equipmentType")).intValue());
            data.setCount(((Number) map.get("count")).intValue());
            result.add(data);
        }
        
        return result;
    }
    
    // 映射区域容量数据
    private List<EquipmentAnalysisVO.ZoneCapacityData> mapZoneCapacityData(List<Map<String, Object>> mapList) {
        List<EquipmentAnalysisVO.ZoneCapacityData> result = new ArrayList<>();
        if (mapList == null || mapList.isEmpty()) {
            return result;
        }
        
        for (Map<String, Object> map : mapList) {
            EquipmentAnalysisVO.ZoneCapacityData data = new EquipmentAnalysisVO.ZoneCapacityData();
            data.setZoneName((String) map.get("zoneName"));
            data.setTotalCapacity(((Number) map.get("totalCapacity")).doubleValue());
            result.add(data);
        }
        
        return result;
    }
}